#!/usr/bin/ruby
require "rubygems"
require "opengl"
require "glut"
require "m3g"
require "pp"


class MyGLUTWidget
  def initialize
    GLUT.Init
    GLUT.InitWindowSize  256, 256
    GLUT.InitDisplayMode GLUT::GLUT_DOUBLE | GLUT::GLUT_RGB | GLUT::GLUT_DEPTH
    GLUT.CreateWindow "Ruby-M3G"

    initialize_scene

    GLUT.ReshapeFunc  method(:reshape).to_proc()
    GLUT.DisplayFunc  method(:display).to_proc()
    GLUT.KeyboardFunc method(:keyboard).to_proc()
    GLUT.MouseFunc    method(:mouse).to_proc()
    GLUT.IdleFunc     method(:idle).to_proc()
  end

  def initialize_scene

    bg = M3G::Background.new 
    bg.color = 0x7f3f3fff

    positions = M3G::VertexArray.new  4, 3, 4
    positions.values[0,4] = [1,-1,0, 1,1,0, -1,-1,0, -1,1,0]
    
    colors = M3G::VertexArray.new  4, 3, 1
    colors.values[0,4] = [255,255,255, 255,0,0, 255,255,255, 0,0,255, 255,255,255]

    scale = 1
    bias  = [0,0,0]
    vertices = M3G::VertexBuffer.new
    vertices.positions = positions, scale, bias
    vertices.colors    = colors

    indices = [0,1,2,3]
    strips  = [4]
    tris = M3G::TriangleStripArray.new indices, strips

    pmode = M3G::PolygonMode.new
    pmode.culling = M3G::PolygonMode::CULL_NONE

    mat = M3G::Material.new
    mat.vertex_color_tracking_enable = true

    app = M3G::Appearance.new
    app.polygon_mode = pmode
    app.material = mat

    cam = M3G::Camera.new
    cam.look_at 0,0,15, 0,0,0, 0,1,0
    
    mesh1 = M3G::Mesh.new vertices, tris, app

    mesh2 = M3G::Mesh.new vertices, tris, app
    mesh2.translate 3, 0, 0

    mesh3 = M3G::Mesh.new vertices, tris, app
    mesh3.translate -3, 0, 0
    

    @wld = M3G::World.new
    @wld.child << cam
    @wld.child << mesh1
    @wld.child << mesh2
    @wld.child << mesh3
    @wld.active_camera = cam
    @wld.background    = bg

    mesh1.alignment = cam, M3G::Node::NONE,   @wld, M3G::Node::NONE
    mesh2.alignment = cam, M3G::Node::ORIGIN, @wld, M3G::Node::ORIGIN
    mesh3.alignment = cam, M3G::Node::ORIGIN, cam, M3G::Node::ORIGIN

  end

  def reshape width, height
    g3d = M3G::Graphics3D::instance
    g3d.viewport = 0,0,width,height
    @wld.active_camera.perspective = 45, width/(height*1.0), 0.1, 100
  end

  def display
    g3d = M3G::Graphics3D::instance
    g3d.render @wld
    GLUT.SwapBuffers()
  end
  
  def keyboard key, x, y
puts key
    if key == 113  # 'q'
      exit
    elsif key == 32 # ' '
    elsif key == 52 # '4'
      cam = @wld.active_camera
      cam.post_rotate 5, 0,1,0
      @wld.align nil
    elsif key == 54 # '6'
      cam = @wld.active_camera
      cam.post_rotate -5, 0,1,0
      @wld.align nil
    elsif key == 56 # '8'
      cam = @wld.active_camera
      cam.post_rotate 5, 1,0,0
      @wld.align nil # '2'
    elsif key == 50
      cam = @wld.active_camera
      cam.post_rotate -5, 1,0,0
      @wld.align nil
    end
    GLUT.PostRedisplay
  end

  def mouse button, state, x, y
  end

  def idle
    GLUT.PostRedisplay
  end

  def start
    GLUT.MainLoop
  end

end


MyGLUTWidget.new.start

